//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.hbzhit.common.utils;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.ProcessDefinition;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

public class WorkflowUtils {
    private static Logger logger = LoggerFactory.getLogger(WorkflowUtils.class);

    public WorkflowUtils() {
    }

    public static String parseToZhType(String type) {
        HashMap<String, String> types = new HashMap<String, String>();
        types.put("userTask", "用户任务");
        types.put("serviceTask", "系统任务");
        types.put("startEvent", "开始节点");
        types.put("endEvent", "结束节点");
        types.put("exclusiveGateway", "条件判断节点(系统自动根据条件处理)");
        types.put("inclusiveGateway", "并行处理任务");
        types.put("callActivity", "子流程");
        return types.get(type) == null?type:(String)types.get(type);
    }

    public static String exportDiagramToFile(RepositoryService repositoryService, ProcessDefinition processDefinition, String exportDir) throws IOException {
        String diagramResourceName = processDefinition.getDiagramResourceName();
        String key = processDefinition.getKey();
        int version = processDefinition.getVersion();
        String diagramPath = "";
        InputStream resourceAsStream = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), diagramResourceName);
        byte[] b = new byte[resourceAsStream.available()];
        @SuppressWarnings("unused")
		boolean len = true;
        resourceAsStream.read(b, 0, b.length);
        String diagramDir = exportDir + "/" + key + "/" + version;
        File diagramDirFile = new File(diagramDir);
        if(!diagramDirFile.exists()) {
            diagramDirFile.mkdirs();
        }

        diagramPath = diagramDir + "/" + diagramResourceName;
        File file = new File(diagramPath);
        if(file.exists()) {
            logger.debug("diagram exist, ignore... : {}", diagramPath);
            return diagramPath;
        } else {
            file.createNewFile();
            logger.debug("export diagram to : {}", diagramPath);
            FileUtils.writeByteArrayToFile(file, b, true);
            return diagramPath;
        }
    }
}

